package com.iplant.basic.serviceImpl;
 
import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.orm.ORMUtils;
import com.iplant.base.utils.sql.orm.PoService;
import com.iplant.basic.service.BQMService;
import com.iplant.basic.service.basicenum.bpm.BPMEventModule;
import com.iplant.basic.service.basicenum.bpm.BPMTaskStatus;
import com.iplant.basic.service.basicenum.bpm.TaskQueryType;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.basic.service.po.bms.qft.BMSPersonQualification;
import com.iplant.basic.serviceImpl.dao.bms.qft.BMSPersonQualificationDAO;
import com.iplant.basic.utils.Constants; 
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.*;

@Slf4j
@Service
public class BQMServiceImpl implements BQMService {

    //region 构造函数
    public BQMServiceImpl() {
        Constants.BPMServiceMap.put(BPMEventModule.PersonQualificationAudit, this);
    }
    //endregion


    //region 单实例

    private static BQMService Instance = null;

    public static BQMService getInstance() {
        if (Instance == null)
            Instance = new BQMServiceImpl();

        return Instance;
    }


    //endregion


    //region 流程
    @Override
    public ServiceResult<BMSPersonQualification> BPM_QueryDefaultTask(BMSEmployee wLoginUser,
                                                                      int wTaskID, int wEventID) {
        ServiceResult<BMSPersonQualification> wResult = new ServiceResult<>();
        wResult.Result = new BMSPersonQualification();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);


            Map<String, Object> wParams = new HashMap<String, Object>();
            wParams.put("ID", wTaskID);

            BMSPersonQualification wEXCUnusualTask = BMSPersonQualificationDAO.getInstance().SelectPO(wLoginUser,
                    wParams, wErrorCode);
            if (wEXCUnusualTask != null && wEXCUnusualTask.ID > 0) {
                wEXCUnusualTask.FlowType = wEventID;
                wEXCUnusualTask.UpFlowID = wLoginUser.ID;
                wEXCUnusualTask.UpFlowName = wLoginUser.Name; 

                wResult.Result = wEXCUnusualTask;
            }

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());

        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            log.error(e.toString());
        }
        return wResult;
    }


    @Override
    public ServiceResult<BMSPersonQualification> BPM_CreateTask(BMSEmployee wLoginUser,
                                                        BPMEventModule wEventID) {
        ServiceResult<BMSPersonQualification> wResult = new ServiceResult<>();
        wResult.Result = new BMSPersonQualification();
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            BMSPersonQualification wPo = new BMSPersonQualification();
            wPo.setUserInfo(wLoginUser);
            wPo.ID = 0;
            wPo.FlowType = wEventID.getValue();
            wPo.UpFlowID = wLoginUser.ID;
            wPo.UpFlowName = wLoginUser.Name;
            wPo.Status = 0;
            wPo.StatusText = "";
            wPo.CreateTime = Calendar.getInstance();
            wPo.EditTime = Calendar.getInstance();


            BMSPersonQualificationDAO.getInstance().UpdatePo(wLoginUser, wPo, wErrorCode);

            wResult.Result = wPo;

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            log.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<BMSPersonQualification> BPM_SubmitTask(BMSEmployee wLoginUser,
                                                        BMSPersonQualification wData) {
        ServiceResult<BMSPersonQualification> wResult = new ServiceResult<>();
        wResult.Result = wData;
        try {
            OutResult<Integer> wErrorCode = new OutResult<>(0);

            if (wData.Status == BPMTaskStatus.Confirm.getValue()) {
                wData.StatusText = BPMTaskStatus.Confirm.getLabel(); 
            }

            wData.setUserInfo(wLoginUser); 
            BMSPersonQualificationDAO.getInstance().UpdatePo(wLoginUser, wData, wErrorCode);

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.toString();
            log.error(e.toString());
        }
        return wResult;
    }

    @Override
    public ServiceResult<BMSPersonQualification> BPM_GetTask(BMSEmployee wLoginUser, int wID) {
        ServiceResult<BMSPersonQualification> wResult = new ServiceResult<>();
        OutResult<Integer> wErrorCode = new OutResult<>(0);
        wResult.Result = BMSPersonQualificationDAO.getInstance().SelectPO(wLoginUser, wID, "", wErrorCode);
        wResult.AddFaultCode(wErrorCode);
        return wResult;
    }

    @Override
    public Class<BMSPersonQualification> GetTClass() {
        return BMSPersonQualification.class;
    }



    @Override
    public ServiceResult<List<BMSPersonQualification>> BPM_QueryEmployeeAllWeb(BMSEmployee wLoginUser, Map<String,
            Object> wParams, Pagination wPagination) {
        ServiceResult<List<BMSPersonQualification>> wResult = new ServiceResult<>();
        wResult.Result = new ArrayList<>();
        OutResult<Integer> wErrorCode = new OutResult<>(0);
        try {
            int wStatus = StringUtils.parseInt(wParams.get("Status"));
            switch (wStatus) {
                case 0://待办
                    wParams.put("Status", "2,3,4,5,6,7");
                    wResult.Result = BMSPersonQualificationDAO.getInstance().SelectPOList(wLoginUser, wParams, wPagination,
                            wErrorCode);
                    break;
                case 1://已办
                    wParams.put("Status", "20,21,22");
                    wResult.Result = BMSPersonQualificationDAO.getInstance().SelectPOList(wLoginUser, wParams, wPagination,
                            wErrorCode);
                    break;
                default://全部
                    wParams.put("Status", "2,3,4,5,6,7,20,21,22");
                    wResult.Result = BMSPersonQualificationDAO.getInstance().SelectPOList(wLoginUser, wParams, wPagination,
                            wErrorCode);
                    break;
            }

            if (wResult.Result.size() <= 0 || wStatus == 1) {
                return wResult;
            }

            // 待办数据处理
            List<BMSPersonQualification> wBaseList =
                    BMSPersonQualificationDAO.getInstance().BFC_GetUndoTaskList(wLoginUser,
                            wLoginUser.ID,
                            new ArrayList<>(Collections.singletonList(BPMEventModule.PersonQualificationAudit.getValue()))
                            , wParams, wPagination, wErrorCode);
            if (wBaseList == null || wBaseList.size() <= 0) {
                return wResult;
            }
            for (BMSPersonQualification wTaskBase : wBaseList) {
                wTaskBase.TagTypes = TaskQueryType.ToHandle.getValue();
                for (int i = 0; i < wResult.Result.size(); i++) {
                    if (wResult.Result.get(i).ID == wTaskBase.ID)
                        wResult.Result.set(i, wTaskBase);
                }
            }

            wResult.setFaultCode(ExceptionTypes.getEnumType(wErrorCode.Result).getLabel());
        } catch (Exception e) {
            wResult.FaultCode += e.getMessage();
            log.error("EXC_QueryPersonQualificationEmployeeAllWeb",e.toString());
        }
        return wResult;
    }


    //endregion
}
